package com.ityuwen.mengyagushi.album.config;

import com.ityuwen.mengyagushi.album.mapper.AlbumInfoMapper;
import com.ityuwen.mengyagushi.common.constant.RedisConstant;
import com.ityuwen.mengyagushi.model.album.AlbumInfo;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class BloomFilterConfig {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private AlbumInfoMapper albumInfoMapper;

//    @Bean
//    public RBloomFilter bloomFilter(){
//        RBloomFilter<String> bloomFilter = this.redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        bloomFilter.tryInit(1000000, 0.03);
//
//        // TODO: 再初始化一个声音的布隆过滤器
//
//        // TODO：由于已经有数据了，所以一次性把已有数据全部放入布隆过滤器
//        List<AlbumInfo> albumInfos = this.albumInfoMapper.selectList(null);
//        albumInfos.forEach(albumInfo -> {
//            bloomFilter.add(RedisConstant.ALBUM_INFO_PREFIX + albumInfo.getId());
//        });
//        return bloomFilter;
//    }

    @PostConstruct
    public void init(){
        RBloomFilter<String> bloomFilter = this.redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        bloomFilter.tryInit(1000000, 0.03);

        // TODO: 再初始化一个声音的布隆过滤器

        // TODO：由于已经有数据了，所以一次性把已有数据全部放入布隆过滤器
        List<AlbumInfo> albumInfos = this.albumInfoMapper.selectList(null);
        albumInfos.forEach(albumInfo -> {
            bloomFilter.add(RedisConstant.ALBUM_INFO_PREFIX + albumInfo.getId());
        });
    }
}
